package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.widget.TimeCount;

import java.sql.Time;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class ModifyMobile1Activity extends BaseActivity {


    public static void start(Context context,String mobile){
        Intent intent = new Intent(context,ModifyMobile1Activity.class);
        intent.putExtra("mobile",mobile);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.btn_code)
    TextView btnCode;
    @BindView(R.id.btn_next)
    TextView btnNext;

    private String mobile ="",code = "";
    private TimeCount timeCount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_mobile1;
    }

    @Override
    protected void initData() {
        initStatusBar(topView,false);
        titleContent.setText("更换手机号");

        mobile = getIntent().getStringExtra("mobile");
        tvMobile.setText("现有手机号："+ViewUtils.changeMobile(mobile));
        timeCount = new TimeCount(60000, 1000, btnCode);

    }

    @Override
    protected void setListener() {

    }

    @OnClick({R.id.title_finish, R.id.btn_code, R.id.btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_code:
                postCode();
                break;
            case R.id.btn_next:
                String code2 = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(code2)){
                    ToastUtils.showToast("请输入验证码");
                    return;
                }

                if (!code2.equals(code)){
                    ToastUtils.showToast("验证码不正确");
                    return;
                }

                startActivity(new Intent(this,ModifyMobile2Activity.class));
                finish();

                break;
        }
    }


    private void postCode() {
        timeCount.start();
        mApiHelper.getMsmCode(mobile, new Observer<BaseBean<String>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(BaseBean<String> baseBean) {
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    code = baseBean.getData();
                } else {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        ViewUtils.closeKeyboard(this);
    }
}
